Skip to content

Fix potential crash when extracting URI path in Keychain#187

Merged
rmarinho merged 1 commit into
mainfrom
fix/keychain-uri-segments-crash
Jun 1, 2026
Merged

Fix potential crash when extracting URI path in Keychain#187
rmarinho merged 1 commit into
mainfrom
fix/keychain-uri-segments-crash

Conversation

@rmarinho
Copy link
Copy Markdown
Member

Summary

The pattern string.Join("", uri.Segments).Substring(1) appeared 4 times in Keychain.cs. It strips the leading / from the URI path, but would throw ArgumentOutOfRangeException for URIs where the joined segments produce a string shorter than 2 characters (e.g. a host-only URI like http://host).

Extracts the logic into a GetUriPathBytes helper that safely handles edge cases, and replaces all inline occurrences.

Testing

Build verified. The affected code paths require macOS keychain access, so no unit test was added.

Copilot AI review requested due to automatic review settings May 29, 2026 15:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors four duplicated, crash-prone inline URI path extractions in Keychain.cs into a single GetUriPathBytes helper that safely handles URIs whose joined segments are shorter than 2 characters (e.g., host-only URIs).

Changes:

  • Adds GetUriPathBytes helper that returns an empty byte array when the URI has no path beyond /.
  • Replaces four string.Join("", uri.Segments).Substring(1) occurrences with calls to the helper.

The pattern string.Join("", uri.Segments).Substring(1) would throw
an ArgumentOutOfRangeException if the joined string was empty.

Extract the logic into a GetUriPathBytes helper that strips exactly one
leading '/' when present and handles edge cases safely, replacing all 4
inline occurrences. Uses Segments-based logic (not Uri.AbsolutePath) to
preserve exact byte-compatibility with existing keychain entries.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rmarinho rmarinho force-pushed the fix/keychain-uri-segments-crash branch from e174d4d to 12da03f Compare May 29, 2026 16:02
@rmarinho rmarinho merged commit a200df6 into main Jun 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants